package com.aaa.system.service.impl;

import com.aaa.entity.Menu;
import com.aaa.system.dao.MenuDao;
import com.aaa.system.service.MenuService;
import com.aaa.utils.JwtUtil;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: his-open-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-03-20 10:58
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findByUserid(Long userid) {
        return menuDao.selectByUserid(userid);
    }

    @Override
    public Result<List<Menu>> getLeftMenu() {
        //获取当前用户的手机号
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        String phone = (String) map.get("username");
        //该用户具有的菜单
        List<Menu> menus = menuDao.findLeftMenu(phone);

        //1.查询所有的一级菜单
        List<Menu> firsts=menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());

        //2.查询一级菜单对应的二级菜单---只适合只有两级菜单
        for(Menu f:firsts){
              f.setChildren(getChildren(f.getMenuId(),menus));
        }

        return new Result<List<Menu>>(2000,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Menu>> getAll() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Menu> menus = menuDao.selectList(wrapper);
        //1.查询所有的一级菜单
        List<Menu> firsts=menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
        //2.查询一级菜单对应的二级菜单---只适合只有两级菜单
        for(Menu f:firsts){
            f.setChildren(getChildren(f.getMenuId(),menus));
        }

        return new Result<List<Menu>>(2000,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Integer>> findByRoleId(Long roleId) {
        List<Integer> menuIds= menuDao.getByRoleId(roleId);
        return new Result<>(2000,"查询选中的菜单id",menuIds);
    }

    @Transactional
    @Override
    public Result<String> confirmMenu(Long roleId, Long[] menuIds) {
        //roleId删除具有的菜单id
        menuDao.deleteByRoleId(roleId);
        //批量添加
        menuDao.batchMenus(roleId,menuIds);
        return new Result<>(2000,"分配成功");
    }

    private List<Menu> getChildren(Long parentId,List<Menu> menus){
        List<Menu> seconds=new ArrayList<>();
        for(Menu m:menus){
             if(m.getParentId().equals(parentId)){
                 seconds.add(m);
             }
        }
        for(Menu m:seconds){
             m.setChildren(getChildren(m.getMenuId(),menus));
        }

        return seconds;

    }
}






















